package com.service;

import java.util.List;

import com.google.android.maps.GeoPoint;

import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteCallbackList;
import android.util.Log;
import android.widget.Toast;

public class BackgroundService extends Service{

	private static final String BROADCAST_NAME = "lns.create.project.broadcast";
	private double latitude, longitude;
	private GeoPoint current_Gp;
	private LocationManager Location_Manager;
	private String providers;
	private List<GeoPoint> list_Geopoint;
	
	public void send(String name)
	{
		
		Intent intent = new Intent(BROADCAST_NAME);
		intent.putExtra("MSG_READ", name);
		sendBroadcast(intent);
	}
	public double getlatitude()
	{
		return latitude;
	}
	
	public double getlongtitude()
	{
		
		return longitude;
	}
	
	public GeoPoint getcurr_Gp()
	{
		return current_Gp;
	}
	public double getDistance(GeoPoint Point)
	{
		double D2R = Math.PI / 180.0;
		double lat1 = Point.getLatitudeE6() / 1E6, lon1 = Point.getLatitudeE6() / 1E6;
		double lat2 = current_Gp.getLatitudeE6() / 1E6 , lon2 = current_Gp.getLongitudeE6() / 1E6;
		double dLat = Math.toRadians(lat1 - lat2);
		double dLon = Math.toRadians(lon1 - lon2);
		double a = Math.pow(Math.sin(dLat / 2.0),2) +
				Math.cos(lat1 * D2R) * Math.cos(lat2 * D2R) * Math.pow(Math.sin(dLon / 2.0), 2);
		double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
		double d = 6367 * c;
		return d;
	}
	@Override
	public IBinder onBind(Intent arg0) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
	}

	@Override
	public void onCreate() {
		// TODO Auto-generated method stub
		
		super.onCreate();
	
	 
			
	}

	@Override
	public int onStartCommand(Intent intent, int flags, int startId) {
		// TODO Auto-generated method stub
		Log.e("BackgroundService", "Service_Started");
		new Thread()
		{

			@Override
			public void run() {
				// TODO Auto-generated method stub
				super.run();
				while(true)
				{
					try {
						Thread.sleep(1000);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					Log.e("Service_logging", "state_good");
				}
			}
			
		}.start();

		return super.onStartCommand(intent, flags, startId);
	}

	public void checkProvider(){
		Location_Manager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
		Criteria cta = new Criteria();
		cta.setAccuracy(Criteria.ACCURACY_HIGH);
		cta.setPowerRequirement(Criteria.POWER_HIGH);
		cta.setAltitudeRequired(false);
		cta.setSpeedRequired(false);
		cta.setCostAllowed(false);
		providers = Location_Manager.getBestProvider(cta, true);
	}



}
